package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

//对外暴露service服务
@DubboService
public class UserInfoApiImpl implements UserInfoApi{

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserInfoApi userInfoApi;

    //保存用户信息
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    //更新用户信息
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    //根据id查看用户信息
    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    //根据id 批量查询用户信息
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> ids, UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.in(UserInfo::getId,ids);
        if (userInfo != null){
            lqw.eq(userInfo.getGender() != null,UserInfo::getGender,userInfo.getGender());
            lqw.lt(userInfo.getAge() != null,UserInfo::getAge,userInfo.getAge());
        }
        List<UserInfo> userInfos = userInfoMapper.selectList(lqw);
        Map<Long,UserInfo> map = CollUtil.fieldValueMap(userInfos,"id");

        return map;
    }

    //根据id 批量查询用户信息
    @Override
    public List<UserInfo> findById(List<Long> ids, UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.in(UserInfo::getId,ids);
        if (userInfo != null){
            lqw.eq(userInfo.getGender() != null,UserInfo::getGender,userInfo.getGender());
            lqw.lt(userInfo.getAge() != null,UserInfo::getAge,userInfo.getAge());
        }
        List<UserInfo> userInfos = userInfoMapper.selectList(lqw);

        return userInfos;

    }
}
